﻿using System.Threading.Tasks;
using GMS.IServices;
using GMS.IServices.Jwt;
using GMS.Models.Jwt;
using GMS.Models.Other;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace GMS.API.Controllers
{
    [ApiController]
    [Route("Token")]
    public class JwtController : ControllerBase
    {
        private readonly IJwtService _jwtService;
        private readonly IUserInfoService _userInfoService;

        public JwtController(IJwtService jwtService, IUserInfoService userInfoService)
        {
            _jwtService = jwtService;
            _userInfoService = userInfoService;
        }

        [HttpPost("Generate")]
        public async Task<IActionResult> GenerateToken(Login login)
        {
            var user = await _userInfoService.LoginAsync(login.UserName, login.PassWord);
            Payload payload = new Payload()
            {
                Id = user.Id,
                Name = user.UserName
            };
            var token = await _jwtService.GenerateTokenAsync(payload);

            return StatusCode(200, new { code = 200, message = "获取Token成功", data = token });
        }

    }
}